<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="供应商编号" prop="supplierNo">
        <el-input
          v-model="queryParams.supplierNo"
          placeholder="请输入供应商编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商类型" prop="supplierType">
        <el-select
          v-model="queryParams.supplierType"
          placeholder="请选择供应商类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.supplier_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合作关系等级" prop="partnershipLevel">
        <el-select
          v-model="queryParams.partnershipLevel"
          placeholder="请选择合作关系等级"
          clearable
        >
          <el-option
            v-for="dict in dict.type.partnership_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supplier:supplier:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supplier:supplier:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="supplierList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="供应商编号" align="center" prop="supplierNo">
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="supplierName">
      </el-table-column>
      <el-table-column label="供应商类型" align="center" prop="supplierType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.supplier_type"
            :value="scope.row.supplierType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="社会信用代码"
        align="center"
        prop="socialCreditCode"
      />
      <el-table-column label="法人" align="center" prop="legalPerson" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column
        label="合作关系等级"
        align="center"
        prop="partnershipLevel"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.partnership_level"
            :value="scope.row.partnership_level"
          />
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column
        label="省份"
        align="center"
        prop="province"
        value="province"
      />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="区县" align="center" prop="county" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supplier:supplier:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supplier:supplier:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="checkData(scope.row)"
            v-hasPermi="['supplier:supplier:remove']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listSupplier,
  getSupplier,
  delSupplier,
  addSupplier,
  updateSupplier,
} from "@/api/supplier/supplier";

export default {
  name: "Supplier",
  dicts: ["supplier_type", "partnership_level"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商信息表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierNo: null,
        supplierName: null,
        supplierType: null,
        partnershipLevel: null,
        // options: options,
        // selectedOptions: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商信息列表 */
    getList() {
      console.log(11111111111);
      this.loading = true;
      listSupplier(this.queryParams).then((response) => {
        this.supplierList = response.rows;
       
        for (let i = 0; i < response.rows.length; i++) {
          this.supplierList[i].city = response.rows[i].city.split(",")[1];
          this.supplierList[i].county = response.rows[i].county.split(",")[1];
          this.supplierList[i].province = response.rows[i].province.split(",")[1];
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplierNo: null,
        supplierName: null,
        supplierType: null,
        socialCreditCode: null,
        legalPerson: null,
        contacts: null,
        phone: null,
        partnershipLevel: null,
        address: null,
        province: null,
        city: null,
        county: null,
        createBy: null,
        createTime: null,
        comment: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      this.title = "添加供应商信息";
      this.$router.push({ path: "/supplier/supplierNew" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSupplier(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商信息";
      });
      console.log();
      this.$router.push({
        path: "/supplier/supplyEdit",
        query: {
          id: row.id,
        },
      });
    },
    // 查看
    checkData(row) {
      this.reset();
      const id = row.id || this.ids;
      getSupplier(id).then((response) => {
        this.form = response.data;
        // this.open = true;
        // this.title = "修改供应商信息";
      });
      this.$router.push({ path: "/supplier/supplyCheck",query: {
          id: row.id,
        }, });
        
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate((valid) => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateSupplier(this.form).then((response) => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addSupplier(this.form).then((response) => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除供应商信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSupplier(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "supplier/supplier/export",
        {
          ...this.queryParams,
        },
        `supplier_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
